package com.android.ailaoshan.picture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.ailaoshan.R;
import com.android.ailaoshan.tools.BaseAdapter;

import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class PictureAdapter extends BaseAdapter<PictureAdapter.ViewHolder> {

    private final String PIC_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Picture" + File.separator;

    private List<String> mDataList;

    public PictureAdapter(Context context) {
        super(context);
    }

    public void notifyDataSetChanged(List<String> dataList) {
        this.mDataList = dataList;
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(getInflater().inflate(R.layout.picture, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String filePath = PIC_PATH + mDataList.get(position);
        File file = new File(filePath);
        if(file.exists()){
            Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath),200,300);
            holder.imageView.setImageBitmap(bitmap);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.collection_ImageView);
        }
    }

}